package com.ktwlsoft.archivemanage.domain.business.businessService.pdf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import com.ktwlsoft.archivemanage.domain.business.businessService.businessInspect.BusinessInspectService;
import com.ktwlsoft.archivemanage.domain.business.businessService.guiBusiness.BusinessGuidanceService;
import com.ktwlsoft.archivemanage.domain.business.businessService.guiBusiness.GuiGuidanceMendListService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.ImgsService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.mend.MendListRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Imgs;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.guiBusiness.MendListGetDetailsResult;
import com.ktwlsoft.archivemanage.payload.mendList.MendListResponse;
import com.ktwlsoft.archivemanage.payload.mendList.MendOptionResponse;
import com.ktwlsoft.archivemanage.payload.mendList.MendSubjectResponse;
import com.ktwlsoft.archivemanage.service.img.FileStorageService;
import com.ktwlsoft.archivemanage.service.utils.DateUtils;
import com.ktwlsoft.archivemanage.service.utils.SmartFileUtils;
import com.ktwlsoft.archivemanage.service.utils.pdf.PDFUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.Inet4Address;
import java.net.URLEncoder;
import java.util.List;

/**
 * PDFService
 *
 * @author zhuhe on 2019/11/6 12:18
 * @version 1.0
 */
@Component
public class PDFService {

    /**整改单*/
    private final MendListRepositoryService mendListRepositoryService;

    /**业务检查*/
    private final BusinessInspectService businessInspectService;

    private final Environment environment;

    /**指导整改*/
    private final GuiGuidanceMendListService guidanceMendListService;

    /**业务指导*/
    private final BusinessGuidanceService businessGuidanceService;

    /**图片业务层接口实现类*/
    private final ImgsService imgsService;

    /**图片相关*/
    private final FileStorageService fileStorageService;

    public PDFService(MendListRepositoryService mendListRepositoryService, BusinessInspectService businessInspectService, Environment environment, GuiGuidanceMendListService guidanceMendListService, BusinessGuidanceService businessGuidanceService, ImgsService imgsService, FileStorageService fileStorageService) {
        this.mendListRepositoryService = mendListRepositoryService;
        this.businessInspectService = businessInspectService;
        this.environment = environment;
        this.guidanceMendListService = guidanceMendListService;
        this.businessGuidanceService = businessGuidanceService;
        this.imgsService = imgsService;
        this.fileStorageService = fileStorageService;
    }

    /**
     * 生成pdf on 2019/11/12 10:54
     *
     * @param type 类型:1.整改单,2.业务检查,3.指导整改,4.业务指导
     * @param bId 业务id
     * @return String 路径
     */
    public String createPDF(Integer type,Long bId) throws Exception {
        if(type == null || (bId == null || bId <= 0)){
            return "";
        }
        String pash = "";
        if(type.equals(1)) {
            pash = mendList(bId);
        }else if(type.equals(2)){
            pash = businessGuidance(bId);
        }else if(type.equals(3)){
            pash = guiMendList(bId);
        }else if(type.equals(4)){
            pash = guidance(bId);
        }
        return pash;
    }

    /**
     * 自查复查生成pdf on 2019/11/12 10:54
     *
     * @param type 类型:1.整改单,2.业务检查,3.指导整改,4.业务指导
     * @param bId 业务id
     * @return String 路径
     */
    public String selfOrRepeatCreatePDF(Integer type,Long bId) throws Exception {
        if(type == null || (bId == null || bId <= 0)){
            return "";
        }
        String pash = "";
        if(type.equals(1)) {
            pash = selfOrRepeatMendList(bId);
        }else if(type.equals(2)){
            pash = selfOrRepeatBusinessGuidance(bId);
        }else if(type.equals(3)){
            pash = guiMendList(bId);
        }else if(type.equals(4)){
            pash = guidance(bId);
        }
        return pash;
    }

    /**
     * 业务检查 on 2019/11/13 14:50
     *
     * @param id id
     * @return String
     */
    private String businessGuidance(Long id) throws Exception{
        JSONObject data = new JSONObject();
        Result result = businessInspectService.getById(id);
        if(result.getData() == null){
            return "";
        }
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(result.getData(), SerializerFeature.WriteMapNullValue));
        data.put("titleName", "业务检查");
        data.put("itemName", "考核指标");
        data.put("id", object.get("id") != null?object.getLong("id"):111111L);
        //考核模板
        data.put("temName", object.get("temName") != null?updateString(object.getString("temName"),null):null);
        //满分
        data.put("fullMark", object.get("fullMark") != null?object.get("fullMark"):null);
        //得分
        data.put("mark", object.get("mark") != null?object.get("mark"):null);
        //整改后得分
        data.put("mendMark", null);
        //指导单号
        data.put("guidNum", object.get("guidanceNum") != null?object.get("guidanceNum"):null);
        //指导人
        data.put("instructor", object.get("creator") != null?object.get("creator"):null);
        //指导时间
        data.put("createdAt", object.getDate("createdAt") != null?DateUtils.dateToStr(object.getDate("createdAt")):null);
        //立档单位
        data.put("archiveUnit", object.get("unitName") != null?updateString(object.getString("unitName"),null):null);
        //地址
        data.put("gpsAddress", object.get("unitAddress") != null?updateString(object.getString("unitAddress"),null):null);
        //档案员
        data.put("archivist", object.get("archivist") != null?object.get("archivist"):null);
        //档案员电话
        data.put("archivistPhone", object.get("archivistPhone") != null?object.get("archivistPhone"):null);
        //指导意见
        data.put("guidAdvice", object.get("guidAdvice") != null?updateString(object.getString("guidAdvice"),35):null);
        //被指导意见
        data.put("unitAdvice", object.get("unitAdvice") != null?updateString(object.getString("unitAdvice"),35):null);
        //指导员签字图片
        if(object.get("instructorSignImg") != null){
            List<JSONObject> instructorSignImg = JSONObject.toJavaObject(object.getJSONArray("instructorSignImg"),List.class);
            if(instructorSignImg != null && instructorSignImg.size() > 0 && instructorSignImg.get(0).get("id") != null) {
                data.put("instructorSignImg", getIp() + instructorSignImg.get(0).getLong("id"));
            }
        }
        //被指导单位签字图片
        if(object.get("unitSignImg") != null){
            List<JSONObject> unitSignImg = JSONObject.toJavaObject(object.getJSONArray("unitSignImg"),List.class);
            if(unitSignImg != null && unitSignImg.size() > 0 && unitSignImg.get(0).get("id") != null) {
                data.put("unitSignImg", getIp() + unitSignImg.get(0).getLong("id"));
            }
        }
        //题目子级
        data.put("mendSubjectList", mendSubjectList(object));
        //未达标项
        data.put("notUpToStandard", notUpToStandard(object));
        return PDFUtils.generatePdf(data,"template.ftl", URLEncoder.encode(object.getString("unitName"),"UTF-8") + ".pdf");
    }

    /**
     * 业务检查 on 2019/11/13 14:50
     *
     * @param id id
     * @return String
     */
    private String selfOrRepeatBusinessGuidance(Long id) throws Exception{
        JSONObject data = new JSONObject();
        Result result = businessInspectService.getById(id);
        if(result.getData() == null){
            return "";
        }
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(result.getData(), SerializerFeature.WriteMapNullValue));
        data.put("titleName", "业务检查");
        data.put("itemName", "考核指标");
        data.put("id", object.get("id") != null?object.getLong("id"):111111L);
        //考核模板
        data.put("temName", object.get("temName") != null?updateString(object.getString("temName"),null):null);
        //满分
        data.put("fullMark", object.get("fullMark") != null?object.get("fullMark"):null);
        //得分
        data.put("mark", object.get("mark") != null?object.getBigDecimal("mark").divide(new BigDecimal("2"),0,BigDecimal.ROUND_DOWN):null);
        //整改后得分
        data.put("mendMark", null);
        //指导单号
        data.put("guidNum", object.get("guidanceNum") != null?object.get("guidanceNum"):null);
        //指导人
        data.put("instructor", object.get("creator") != null?object.get("creator"):null);
        //指导时间
        data.put("createdAt", object.getDate("createdAt") != null?DateUtils.dateToStr(object.getDate("createdAt")):null);
        //立档单位
        data.put("archiveUnit", object.get("unitName") != null?updateString(object.getString("unitName"),null):null);
        //地址
        data.put("gpsAddress", object.get("unitAddress") != null?updateString(object.getString("unitAddress"),null):null);
        //档案员
        data.put("archivist", object.get("archivist") != null?object.get("archivist"):null);
        //档案员电话
        data.put("archivistPhone", object.get("archivistPhone") != null?object.get("archivistPhone"):null);
        //指导意见
        data.put("guidAdvice", object.get("guidAdvice") != null?updateString(object.getString("guidAdvice"),35):null);
        //被指导意见
        data.put("unitAdvice", object.get("unitAdvice") != null?updateString(object.getString("unitAdvice"),35):null);
        //指导员签字图片
        if(object.get("instructorSignImg") != null){
            List<JSONObject> instructorSignImg = JSONObject.toJavaObject(object.getJSONArray("instructorSignImg"),List.class);
            if(instructorSignImg != null && instructorSignImg.size() > 0 && instructorSignImg.get(0).get("id") != null) {
                data.put("instructorSignImg", getIp() + instructorSignImg.get(0).getLong("id"));
            }
        }
        //被指导单位签字图片
        if(object.get("unitSignImg") != null){
            List<JSONObject> unitSignImg = JSONObject.toJavaObject(object.getJSONArray("unitSignImg"),List.class);
            if(unitSignImg != null && unitSignImg.size() > 0 && unitSignImg.get(0).get("id") != null) {
                data.put("unitSignImg", getIp() + unitSignImg.get(0).getLong("id"));
            }
        }
        //题目子级
        data.put("mendSubjectList", mendSubjectList(object));
        //未达标项
        data.put("notUpToStandard", notUpToStandard(object));
        return PDFUtils.generatePdf(data,"selfOrRepeatTemplate.ftl", URLEncoder.encode(object.getString("unitName"),"UTF-8") + ".pdf");
    }

    /**
     * 整改单 on 2019/11/13 14:49
     *
     * @param id id
     * @return String
     */
    private String mendList(Long id) throws Exception{
        JSONObject data = new JSONObject();
        MendListResponse response = mendListRepositoryService.findDetail(id);
        if(response == null){
            return "";
        }
        data.put("titleName", "检查整改");
        data.put("id", response.getId() != null?response.getId():222222L);
        //考核模板
        data.put("temName", response.getTemName());
        //满分
        data.put("fullMark", response.getFullMark());
        //得分
        data.put("mark", response.getMark());
        //整改后得分
        data.put("mendMark", response.getMendMark());
        //检查单号
        data.put("guidNum", response.getGuidNum());
        //检查人
        data.put("instructor", response.getInstructor());
        //检查时间
        data.put("createdAt", DateUtils.dateToStr(response.getCreatedAt()));
        //立档单位
        data.put("archiveUnit", response.getArchiveUnit());
        //考核地理位置
        data.put("gpsAddress", updateString(response.getUnitAddress(),20));
        //档案员
        data.put("archivist", response.getArchivist());
        //档案员电话
        data.put("archivistPhone", response.getArchivistPhone());
        //未达标项
        data.put("notUpToStandard", notUpToStandard(response.getMendSubjectList()));
        //整改说明
        data.put("mendRemark", response.getMendRemark() != null?updateString(response.getMendRemark(),40):"");
        //验证备注
        data.put("verifyRemark", response.getMendAdvice() != null?updateString(response.getMendAdvice(),40):"");
        //检查附件
        data.put("additions", response.getAdditions() != null?response.getAdditions():null);
        return PDFUtils.generatePdf(data,"mendListTemplate.ftl", URLEncoder.encode(response.getArchiveUnit(),"UTF-8") + ".pdf");
    }

    /**
     * 自查复查整改单 on 2019/11/13 14:49
     *
     * @param id id
     * @return String
     */
    private String selfOrRepeatMendList(Long id) throws Exception{
        JSONObject data = new JSONObject();
        MendListResponse response = mendListRepositoryService.findDetail(id);
        if(response == null){
            return "";
        }
        data.put("titleName", "检查整改");
        data.put("id", response.getId() != null?response.getId():222222L);
        //考核模板
        data.put("temName", response.getTemName());
        //满分
        data.put("fullMark", response.getFullMark());
        //得分
        data.put("mark", response.getMark().divide(new BigDecimal("2"),0,BigDecimal.ROUND_DOWN));
        //整改后得分
        data.put("mendMark", response.getMendMark());
        //检查单号
        data.put("guidNum", response.getGuidNum());
        //检查人
        data.put("instructor", response.getInstructor());
        //检查时间
        data.put("createdAt", DateUtils.dateToStr(response.getCreatedAt()));
        //立档单位
        data.put("archiveUnit", response.getArchiveUnit());
        //考核地理位置
        data.put("gpsAddress", updateString(response.getUnitAddress(),20));
        //档案员
        data.put("archivist", response.getArchivist());
        //档案员电话
        data.put("archivistPhone", response.getArchivistPhone());
        //未达标项
        data.put("notUpToStandard", notUpToStandard(response.getMendSubjectList()));
        //整改说明
        data.put("mendRemark", response.getMendRemark() != null?updateString(response.getMendRemark(),40):"");
        //验证备注
        data.put("verifyRemark", response.getMendAdvice() != null?updateString(response.getMendAdvice(),40):"");
        //检查附件
        data.put("additions", response.getAdditions() != null?response.getAdditions():null);
        return PDFUtils.generatePdf(data,"selfOrRepeatMendListTemplate.ftl", URLEncoder.encode(response.getArchiveUnit(),"UTF-8") + ".pdf");
    }

    /**
     * 指导整改 on 2019/11/13 14:49
     *
     * @param id id
     * @return String
     */
    private String guiMendList(Long id) throws Exception{
        Result result = guidanceMendListService.getDetails(id);
        if(result.getData() == null){
            return "";
        }
        MendListGetDetailsResult response = (MendListGetDetailsResult)result.getData();
        if(response == null){
            return "";
        }
        JSONObject data = new JSONObject();
        data.put("titleName", "指导整改");
        //指导单号
        data.put("guidanceNum", response.getGuidanceNum() != null?response.getGuidanceNum():"");
        //地理位置
        data.put("geoLocation", response.getGeoLocation() != null?response.getGeoLocation():"");
        //档案员
        data.put("archievist", response.getArchievist() != null?response.getArchievist():"");
        //档案员电话
        data.put("archievistPhone", response.getArchievistPhone() != null?response.getArchievistPhone():"");
        //立档单位
        data.put("unit", response.getUnit() != null?response.getUnit():"");
        //单位地址
        data.put("unitLocation", response.getUnitLocation() != null?response.getUnitLocation():"");
        //整改状态
        String mendState = "";
        if(response.getMendState() != null){
            if(response.getMendState().equals(1)){
                mendState = "待整改";
            }else if(response.getMendState().equals(2)){
                mendState = "待验证";
            }else if(response.getMendState().equals(3)){
                mendState = "打回待整改";
            }else if(response.getMendState().equals(4)){
                mendState = "验证通过";
            }
        }
        data.put("mendState", mendState);
        //标题
        data.put("title", response.getTitle() != null?updateString(response.getTitle(),30):"");
        //表述
        data.put("content", response.getContent() != null?updateString(response.getContent(),40):"");
        //指导条目
        if(response.getItemList() != null){
            response.getItemList().forEach(value->{
                //条目名称
                value.setName(value.getName() != null?updateString(value.getName(),30):"");
                //条目内容
                value.setContent(value.getContent() != null?updateString(value.getContent(),30):"");
                //整改说明
                value.setMendRemark(value.getMendRemark() != null?updateString(value.getMendRemark(),30):"");
                //条目图片
                if(value.getImgList() != null){
                    value.getImgList().forEach(img->{
                        if(img.getSuffix() != null && "mp4".equals(img.getSuffix())){
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(img.getUrl()));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            img.setUrl(getUrlLocate(path));
                        }else {
                            img.setUrl(getIp() + img.getId());
                        }
                    });
                }
                //整改图片
                if(value.getMendListImgList() != null){
                    value.getMendListImgList().forEach(mendImg->{
                        if(mendImg.getSuffix() != null && "mp4".equals(mendImg.getSuffix())){
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(mendImg.getUrl()));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            mendImg.setUrl(getUrlLocate(path));
                        }else {
                            mendImg.setUrl(getIp() + mendImg.getId());
                        }
                    });
                }
            });
        }
        data.put("itemList", response.getItemList() != null?response.getItemList():"");
        //整改说明
        data.put("mendRemark", response.getMendRemark() != null?updateString(response.getMendRemark(),30):"");
        //验证说明
        data.put("verifyRemark", response.getVerifyRemark() != null?updateString(response.getVerifyRemark(),30):"");
        //整改附件
        data.put("additions",response.getAdditions() != null?response.getAdditions():null);
        data.put("id", id);
        return PDFUtils.generatePdf(data,"GuiMendListTemplate.ftl", URLEncoder.encode(response.getUnit(),"UTF-8") + ".pdf");
    }

    /**
     * 业务指导 on 2019/11/13 14:50
     *
     * @param id id
     * @return String
     */
    private String guidance(Long id) throws Exception{
        JSONObject data = new JSONObject();
        Result result = businessGuidanceService.getDetails(id);
        if(result.getData() == null){
            return "";
        }
        JSONObject object = JSONObject.parseObject(JSON.toJSONString(result.getData(), SerializerFeature.WriteMapNullValue));
        data.put("titleName", "业务指导");
        data.put("id", object.get("id") != null?object.getLong("id"):111111L);
        //指导单号
        data.put("guidanceNum", object.get("guidanceNum") != null?object.get("guidanceNum"):null);
        //立档单位
        data.put("unit", object.get("unit") != null?object.get("unit"):null);
        //单位地址
        data.put("unitLocation", object.get("unitLocation") != null?object.get("unitLocation"):null);
        //档案员
        data.put("archievist", object.get("archievist") != null?object.get("archievist"):null);
        //档案员电话
        data.put("archievistPhone", object.get("archievistPhone") != null?object.get("archievistPhone"):null);
        //标题
        data.put("title", object.get("title") != null?updateString(object.getString("title"),30):null);
        //概述
        data.put("content", object.get("content") != null?updateString(object.getString("content"),30):null);
        //指导意见
        data.put("advice", object.get("advice") != null?updateString(object.getString("advice"),40):null);
        //被指导意见
        data.put("guidanceAdvice", object.get("guidanceAdvice") != null?updateString(object.getString("guidanceAdvice"),40):null);
        //指导员签字图片
        Imgs adviceImgSign = null;
        if(object.get("adviceImgSign") != null && object.getLong("adviceImgSign") != 0){
            adviceImgSign = imgsService.getById(object.getLong("adviceImgSign"));
        }
        data.put("adviceImgSign", adviceImgSign != null?getIp() + adviceImgSign.getId():null);
        //被指导单位签字图片
        Imgs guideAdviceSign = null;
        if(object.get("guideAdviceSign") != null){
            guideAdviceSign = imgsService.getById(object.getLong("guideAdviceSign"));
        }
        data.put("guideAdviceSign", guideAdviceSign != null?getIp() + guideAdviceSign.getId():null);
        //指导条目
        data.put("itemList", handleItemList(object));
        return PDFUtils.generatePdf(data,"GuidanceTemplate.ftl", URLEncoder.encode(object.getString("unit"),"UTF-8") + ".pdf");
    }

    /**
      * 处理指导条目 on 2020/9/14 15:35
      *
      * @param object object
      * @return List<JSONObject>
      */
    private List<JSONObject> handleItemList(JSONObject object){
        List<JSONObject> retList = Lists.newArrayList();
        if(object.get("itemList") != null){
            List<JSONObject> list = JSONObject.toJavaObject(object.getJSONArray("itemList"),List.class);
            for(JSONObject jsonObject:list){
                if(jsonObject.get("imgList") != null){
                    List<JSONObject> imgs = JSONObject.toJavaObject(jsonObject.getJSONArray("imgList"),List.class);
                    for(JSONObject object1:imgs){
                        if(object1.get("suffix") != null && "mp4".equals(object1.get("suffix"))){
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(object1.getString("url")));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            object1.put("url", path != null ? getUrlLocate(path) : null);
                        }else {
                            object1.put("url", object1.get("url") != null ? getIp() + object1.getLong("id") : null);
                        }
                    }
                    jsonObject.put("imgList",imgs);
                }
                jsonObject.put("content",jsonObject.get("content") != null?updateString(jsonObject.getString("content"),31):null);
                jsonObject.put("name",jsonObject.get("name") != null?updateString(jsonObject.getString("name"),30):null);
                retList.add(jsonObject);
            }
        }
        return retList;
    }

    /**
     * 处理未达标项 on 2019/11/14 15:57
     *
     * @param object object
     * @return List<JSONObject>
     */
    private List<JSONObject> notUpToStandard(JSONObject object){
        List<JSONObject> retList = Lists.newArrayList();
        if(object.get("subjects") != null){
            List<JSONObject> list = JSONObject.toJavaObject(object.getJSONArray("subjects"),List.class);
            for(JSONObject jsonObject:list){
                jsonObject.put("description",jsonObject.get("description") != null && !"".equals(jsonObject.getString("description"))?updateString(jsonObject.getString("description"),20):null);
                if(jsonObject.get("imgsList") != null){
                    List<JSONObject> imgs = JSONObject.toJavaObject(jsonObject.getJSONArray("imgsList"),List.class);
                    List<Imgs> imgsList = Lists.newArrayList();
                    for (JSONObject object1 : imgs) {
                        Imgs imgs1 = new Imgs();
                        imgs1.setId(object1.get("id") != null ? object1.getLong("id") : null);
                        if (object1.get("suffix") != null && "mp4".equals(object1.get("suffix"))) {
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(object1.getString("url")));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            imgs1.setUrl(getUrlLocate(path));
                        } else {
                            imgs1.setUrl(getIp() + imgs1.getId());
                        }
                        imgsList.add(imgs1);
                    }
                    jsonObject.put("imgs",imgsList);
                }
                if(jsonObject.getInteger("defaultMark") < jsonObject.getInteger("mark")){
                    if(jsonObject.get("itemList") != null){
                        List<JSONObject> jsonObjects = JSONObject.toJavaObject(jsonObject.getJSONArray("itemList"),List.class);
                        for(JSONObject object1:jsonObjects){
                            if(object1.get("imgsList") != null){
                                List<JSONObject> jsonObjects1 = JSONObject.toJavaObject(object1.getJSONArray("imgsList"),List.class);
                                for(JSONObject object2:jsonObjects1){
                                    if(object2.get("suffix") != null && "mp4".equals(object2.get("suffix"))){
                                        String path = null;
                                        try {
                                            path = SmartFileUtils.videoImage(fileStorageService.getStrPath(object2.getString("url")));
                                        }catch (Exception e){
                                            e.printStackTrace();
                                        }
                                        object2.put("url", path != null ? getUrlLocate(path) : null);
                                    }else {
                                        object2.put("url",object2.get("id") != null?getIp() + object2.getLong("id"):null);
                                    }
                                }
                            }
                        }
                    }
                    retList.add(jsonObject);
                }
            }
        }
        return retList;
    }

    /**
      * 处理未达标项 on 2020/9/16 11:18
      *
      * @param mendSubjectResponses mendSubjectResponses
      * @return List<MendSubjectResponse>
      */
    private List<MendSubjectResponse> notUpToStandard(List<MendSubjectResponse> mendSubjectResponses){
        List<MendSubjectResponse> ret = Lists.newArrayList();
        if(mendSubjectResponses == null || mendSubjectResponses.size() <= 0){
            return ret;
        }
        for(MendSubjectResponse responses:mendSubjectResponses){
            responses.setTitle(updateString(responses.getTitle(),30));
            responses.setSubTitle(updateString(responses.getSubTitle(),30));
            responses.setMendRemark(updateString(responses.getMendRemark(),30));
            responses.setRemark(updateString(responses.getRemark(),30));
            responses.setDescription(responses.getDescription() != null && !"".equals(responses.getDescription())?updateString(responses.getDescription(),35):null);
            if(responses.getDefaultMark().compareTo(responses.getMark()) == -1){
                if(responses.getImgs() != null && responses.getImgs().size() > 0){
                    for(Imgs imgs:responses.getImgs()){
                        if(imgs.getSuffix() != null && "mp4".equals(imgs.getSuffix())){
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(imgs.getUrl()));
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                            imgs.setUrl(getUrlLocate(path));
                        }else {
                            imgs.setUrl(getIp() + imgs.getId());
                        }
                    }
                }
                if(responses.getMendOptionResponseList() != null && responses.getMendOptionResponseList().size() > 0){
                    for(MendOptionResponse optionResponse:responses.getMendOptionResponseList()){
                        if(optionResponse.getImgs() != null && optionResponse.getImgs().size() > 0){
                            for(Imgs imgs:optionResponse.getImgs()){
                                if(imgs.getSuffix() != null && "mp4".equals(imgs.getSuffix())){
                                    String path = null;
                                    try {
                                        path = SmartFileUtils.videoImage(fileStorageService.getStrPath(imgs.getUrl()));
                                    }catch (Exception e){
                                        e.printStackTrace();
                                    }
                                    imgs.setUrl(getUrlLocate(path));
                                }else {
                                    imgs.setUrl(getIp() + imgs.getId());
                                }
                            }
                        }
                    }
                }
                ret.add(responses);
            }
        }
        return ret;
    }

    /**
      * 处理题目子级 on 2019/11/14 15:59
      *
      * @param object object
      * @return List<MendSubjectResponse>
      */
    private List<MendSubjectResponse> mendSubjectList(JSONObject object){
        List<MendSubjectResponse> mendSubjectList = Lists.newArrayList();
        if(object.get("subjects") != null){
            List<JSONObject> list = JSONObject.toJavaObject(object.getJSONArray("subjects"),List.class);
            for(JSONObject jsonObject:list){
                MendSubjectResponse subjectResponse = new MendSubjectResponse();
                subjectResponse.setTitle(jsonObject.get("title") != null?updateString(jsonObject.getString("title"),20):null);
                subjectResponse.setDefaultMark(jsonObject.get("defaultMark") != null?jsonObject.getBigDecimal("defaultMark"):null);
                subjectResponse.setRemark(jsonObject.get("remark") != null?updateString(jsonObject.getString("remark"),20):null);
                subjectResponse.setMark(jsonObject.get("mark") != null?jsonObject.getBigDecimal("mark"):null);
                subjectResponse.setDescription(jsonObject.get("description") != null && !"".equals(jsonObject.getString("description"))?updateString(jsonObject.getString("description"),20):null);
                if(jsonObject.get("imgsList") != null){
                    List<JSONObject> imgs = JSONObject.toJavaObject(jsonObject.getJSONArray("imgsList"),List.class);
                    List<Imgs> imgsList = Lists.newArrayList();
                    for (JSONObject object1 : imgs) {
                        Imgs imgs1 = new Imgs();
                        imgs1.setId(object1.get("id") != null ? object1.getLong("id") : null);
                        if (object1.get("suffix") != null && "mp4".equals(object1.get("suffix"))) {
                            String path = null;
                            try {
                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(object1.getString("url")));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            imgs1.setUrl(getUrlLocate(path));
                        } else {
                            imgs1.setUrl(getIp() + imgs1.getId());
                        }
                        imgsList.add(imgs1);
                    }
                    subjectResponse.setImgs(imgsList);
                }
                if(jsonObject.get("itemList") != null){
                    List<JSONObject> itemList = JSONObject.toJavaObject(jsonObject.getJSONArray("itemList"),List.class);
                    if(itemList != null) {
                        List<MendOptionResponse> mendOptionResponses = Lists.newArrayList();
                        for (JSONObject item : itemList) {
                            MendOptionResponse response = new MendOptionResponse();
                            response.setChecked(item.get("checked") != null?item.getByte("checked"):null);
                            response.setOptionName(item.get("option") != null?updateString(item.getString("option"),20):null);
                            response.setOptionMark(item.get("optionMark") != null?item.getBigDecimal("optionMark"):null);
                            if(item.get("imgsList") != null){
                                List<JSONObject> imgsList = JSONObject.toJavaObject(item.getJSONArray("imgsList"),List.class);
                                if(imgsList != null) {
                                    List<Imgs> list1 = Lists.newArrayList();
                                    for (JSONObject object1:imgsList) {
                                        Imgs imgs = new Imgs();
                                        imgs.setId(object1.get("id") != null?object1.getLong("id"):null);
                                        if(object1.get("suffix") != null && "mp4".equals(object1.get("suffix"))){
                                            String path = null;
                                            try {
                                                path = SmartFileUtils.videoImage(fileStorageService.getStrPath(object1.getString("url")));
                                            }catch (Exception e){
                                                e.printStackTrace();
                                            }
                                            imgs.setUrl(getUrlLocate(path));
                                        }else {
                                            imgs.setUrl(getIp() + imgs.getId());
                                        }
                                        list1.add(imgs);
                                    }
                                    response.setImgs(list1);
                                }
                            }
                            mendOptionResponses.add(response);
                        }
                        subjectResponse.setMendOptionResponseList(mendOptionResponses);
                    }
                }
                mendSubjectList.add(subjectResponse);
            }
        }
        return mendSubjectList;
    }

    /**
      * 获取通过url获取文件方法web地址 on 2020/9/15 11:20
      *
      * @param path path
      * @return String String
      */
    private String getUrlLocate(String path){
        String s = "";
        try {
            String ip = Inet4Address.getLocalHost().getHostAddress();
            String port = environment.getProperty("local.server.port");
            s = "http://" + ip + ":" + port + "/api/currencyImg/getFileUrlView?url=" + URLEncoder.encode(path,"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }

    private String getIp(){
        String s = "";
        try {
            String ip = Inet4Address.getLocalHost().getHostAddress();
            String port = environment.getProperty("local.server.port");
            s = "http://" + ip + ":" + port + "/api/currencyImg/getFileView/";
        }catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }

    /**
      * 处理字符 on 2019/11/14 18:42
      *
      * @param str str
      * @param num num
      * @return String
      */
    private String updateString(String str,Integer num){
        if(str == null || "".equals(str)){
            return "";
        }
        StringBuilder sb = new StringBuilder(str);
        if(str.length() > 10){
            for(int i = 0,x = 0;i < sb.length();i++,x++){
                if(x == (num != null?num:10)){
                    sb.insert(i, "<br></br>");
                    x = -9;
                }
            }
        }
        return sb.toString();
    }
}